﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace AppUpgrader
{
    public class UpdateManifest
    {
        private XmlDocument m_xDoc = null;
        public VersionManifest PatchManifest { get; private set; }
        public VersionManifest BaseManifest { get; private set; }

        public UpdateManifest(XmlDocument xDoc)
        {
            m_xDoc = xDoc;
            ParseManifest();
        }

        private void ParseManifest()
        {
            if (this.m_xDoc == null) return;
            XmlElement root = m_xDoc.DocumentElement;
            XmlElement vpatch = root.SelectSingleNode("patch-version") as XmlElement;
            XmlElement vBase = root.SelectSingleNode("base-version") as XmlElement;

            if (vpatch != null)
            {
                this.PatchManifest = new VersionManifest(vpatch);
            }
            if (vBase == null)
                throw new Exception("Can not find base version section from update manifest file");
            this.BaseManifest = new VersionManifest(vBase);
        }
    }

    public class VersionManifest
    {
        public String Name { get; private set; }
        public Double Version { get; private set; }
        public int Size { get; private set; }
        public String location { get; private set; }

        public VersionManifest(XmlElement el)
        {
            Parse(el);
        }

        private void Parse(XmlElement el)
        {
            if (el == null) return;
            XmlNode NameNode = el.SelectSingleNode("name");
            XmlNode VersionNode = el.SelectSingleNode("version");
            XmlNode SizeNode = el.SelectSingleNode("size");
            XmlNode LocationNode = el.SelectSingleNode("location");

            if (NameNode != null)
            {
                this.Name = NameNode.InnerText;
            }
            if (SizeNode != null)
            {
                String StringVal = SizeNode.InnerText;
                int val = 0;
                Int32.TryParse(StringVal,out val);
                this.Size = val;
            }
            if (VersionNode == null)
                throw new Exception("Can not find version node from update manifest file");
            if (LocationNode == null)
                throw new Exception("Can not find location node from update manifest file");

            String StringVer = VersionNode.InnerText;
            if (String.IsNullOrWhiteSpace(StringVer))
                throw new Exception("Can not find version value from update manifest file");
            double dval = 0;
            if(!Double.TryParse(StringVer, out dval))
                throw new Exception("Invalid version value:" + StringVer);
            this.Version = dval;

            String loc = LocationNode.InnerText;
            if (String.IsNullOrWhiteSpace(loc))
                throw new Exception("Can not find location value from update manifest file");
            this.location = loc;
        }
    }
}
